package com.qjq.glasses_shopping.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qjq.glasses_shopping.mapper.ProductTypeMapper;
import com.qjq.glasses_shopping.pojo.ProductType;
import com.qjq.glasses_shopping.result.BusException;
import com.qjq.glasses_shopping.result.CodeEnum;
import com.qjq.glasses_shopping.service.ProductTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
@Service
@Transactional
public class ProductTypeServiceImpl implements ProductTypeService {
    @Autowired
    private ProductTypeMapper productTypeMapper;

    @Override
    public void add(ProductType productType) {
        //根据父类型查询父类型
        ProductType productTypeParent = productTypeMapper.selectById(productType.getId());
        if (productTypeParent == null) {//如果父类型为空那么为一级类型
            productType.setLevel(1);
        } else if (productTypeParent.getLevel() < 3) {//如果父类型在1-3之间,则为父类型+1
            productType.setLevel(productTypeParent.getLevel() + 1);
        } else if (productTypeParent.getLevel() == 3) {//如果父类型等于3级则抛出异常
            throw new BusException(CodeEnum.ADD_PRODUCT_TYPE_ERROR);
        }
        productTypeMapper.insert(productType);
    }

    @Override
    public void update(ProductType productType) {
        //根据父类型查询父类型
        ProductType productTypeParent = productTypeMapper.selectById(productType.getId());
        if (productTypeParent == null) {//如果父类型为空那么为一级类型
            productType.setLevel(1);
        } else if (productTypeParent.getLevel() < 3) {//如果父类型在1-3之间,则为父类型+1
            productType.setLevel(productTypeParent.getLevel() + 1);
        } else if (productTypeParent.getLevel() == 3) {//如果父类型等于3级则抛出异常
            throw new BusException(CodeEnum.ADD_PRODUCT_TYPE_ERROR);
        }
        productTypeMapper.updateById(productType);
    }

    @Override
    public void delete(Long id) {
        //判断是否含有 子类型 有则不能删除
        QueryWrapper<ProductType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parentId", id);
        List<ProductType> productTypeList = productTypeMapper.selectList(queryWrapper);
        //如果有子类型删除失败
        if (productTypeList == null) {
            throw new BusException(CodeEnum.DELETE_PRODUCT_TYPE_ERROR);
        }
        productTypeMapper.deleteById(id);
    }

    @Override
    public ProductType findById(Long id) {
        return productTypeMapper.selectById(id);
    }

    @Override
    public Page<ProductType> search(ProductType productType, int page, int size) {
        QueryWrapper<ProductType> queryWrapper = new QueryWrapper<>();
        if (productType != null) {
            //当用户名不为空
            if (StringUtils.hasText(productType.getName())) {
                queryWrapper.like("name", productType.getName());
            }
            //上级id不为空时
            if (productType.getParentId() != null) {
                queryWrapper.eq("parentId", productType.getParentId());
            }


        }
        return productTypeMapper.selectPage(new Page<>(page, size), queryWrapper);
    }

    @Override
    public List<ProductType> findProductType(ProductType productType) {
        QueryWrapper<ProductType> queryWrapper = new QueryWrapper<>();
        if (productType != null) {
            //当用户名不为空
            if (StringUtils.hasText(productType.getName())) {
                queryWrapper.like("name", productType.getName());
            }
            //上级id不为空时
            if (productType.getParentId() != null) {
                queryWrapper.eq("parentId", productType.getParentId());
            }

        }
        return productTypeMapper.selectList(queryWrapper);
    }
}

